package org.bakerstreet.components.render;

import java.awt.event.ComponentEvent;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.bakerstreet.components.CheckBox;
import org.bakerstreet.components.ComboBox;
import org.bakerstreet.components.Component;
import org.bakerstreet.components.EditBox;
import org.bakerstreet.components.Label;
import org.bakerstreet.components.TextArea;
import org.bakerstreet.form.Form;
import org.bakerstreet.servlets.ScriptBuffer;

public class RenderManager {
	private static Map<Class<? extends Component>, ComponentRender> renderMap;
	
	
	static{
		
		renderMap = new HashMap<Class<? extends Component>,ComponentRender>();
		renderMap.put(EditBox.class, new EditBoxRender());
		renderMap.put(ComboBox.class, new ComboboxRender());
		renderMap.put(CheckBox.class, new CheckBoxRender());
		renderMap.put(TextArea.class, new TextAreaRender());
		renderMap.put(Label.class, new LabelRender());
	}
	
	
	public void renderForm(Form form, ScriptBuffer writer){
		StringBuffer sb =  new StringBuffer();
		
		renderTable(sb, form.getRowCount(), form.getColumnCount());
		
		renderComponents(form, sb);
		
		writer.write(sb.toString());
		
	}
	
	public void updateForm(Form form, StringBuffer writer){
		StringBuffer sb =  new StringBuffer();
		
		Map<String, Component> components = form.getChanged();
		
		Set<String> set = components.keySet();
		
		for (String compId : set) {
			Component comp = components.get(compId);
			if (comp.hasChanges()){
				ComponentRender compRender = renderMap.get(comp.getClass());
				sb.append(" \n ");
				compRender.update(comp, sb);
			}
		}
		
		form.setChanged(new HashMap<String, Component>());
		
		writer.append(sb.toString());
		
	}
	
	private void renderComponents(Form form, StringBuffer sb){
		Map<String, Component> components = form.getComponents();
		
		Set<String> set = components.keySet();
		
		for (String compId : set) {
			Component comp = components.get(compId);
			ComponentRender compRender = renderMap.get(comp.getClass());
			sb.append(" \n ");
			compRender.render(comp, sb);
		}
	}
	
	private void renderTable(StringBuffer sb, int rows, int cols){
		sb.append("formArea.innerHTML = '");
		sb.append(beginTable());
		for (int row = 0; row < rows; row++) {
			sb.append(beginTr());
			for (int col = 0; col < cols; col++) {
				boolean isHeader = (row == 0 || col == 0) ? true : false;
				StringBuffer id = new StringBuffer();
				id.append("$").append(row).append("$").append(col);
				sb.append(beginTd(id.toString(),isHeader));
				sb.append(endTd());
			}
			sb.append(endTr());
		}
		sb.append(endTable());
		sb.append(" '; ");
	}
	
	
	public String beginTable(){
		return "<table id=\"$result_table\" cellspacing=\"0\" class=\"table-data\" cellpadding=\"0\" >";
	}
	
	public String endTable(){
		return "</table>";
	}
	
	public String beginTr(){
		return "<tr>";
	}
	
	public String endTr(){
		return "</tr>";
	}
	
	public String beginTd(String id, boolean isHeader){
		String className = isHeader ? "header-cell" : "data-cell";
		return "<td id=\""+id+"\" align=\"center\" class=\""+className+"\" >";
	}
	
	public String endTd(){
		return "</td>";
	}
	
}
